package com.lucenly.card.reciver;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.lucenly.card.utils.AdvancedCountdownTimer;
import com.lucenly.card.utils.TimeUtils;

public class CountService extends Service{
    long millisInFuture;
    Long beijingTime;
    AdvancedCountdownTimer timer;
    Handler mHandler = new Handler();
    TimeThread timeThread;
    Thread thread;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("CountService","onCreate==="+beijingTime);
        timeThread = new TimeThread();
    }
    @Override
    public void onStart(final Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("CountService","onStart===");
        if(intent==null){
            return;
        }
        millisInFuture = intent.getLongExtra("millisInFuture",0);
        if(millisInFuture==0){
            return;
        }
        thread = new Thread() {
            @Override
            public void run() {
                beijingTime = intent.getLongExtra("time",0);
                if(timer!=null){
                    timer.cancel();
                }
                timer = new AdvancedCountdownTimer(millisInFuture,1000) {
                    @Override
                    public void onTick(long millisUntilFinished, int percent) {//48960000
                        Intent tickIntent = new Intent();
                        tickIntent.setAction("onTick");
                        tickIntent.putExtra("millisUntilFinished",millisUntilFinished);
                        sendBroadcast(tickIntent);
                    }

                    @Override
                    public void onFinish() {
                        Log.e("onFinish","onFinish");
                        Intent finishIntent = new Intent();
                        finishIntent.setAction("onFinish");
                        sendBroadcast(finishIntent);
                        if(timer!=null){
                            timer.cancel();
                        }
                        timer.setmTotalTime(24*60*60*1000);
                        timer.start();
                    }
                };
                timer.start();
                if(!timeThread.isAlive()){
                    timeThread.start();
                }
            }
        };
        thread.start();
    }
    @Override
    public void onDestroy() {
        Log.e("CountService","onDestroy===");
        if(timer!=null){
            timer.cancel();
        }
        if(timeThread!=null&&timeThread.isAlive()){
            timeThread.stop();
        }
        if(thread!=null){
            thread.stop();
        }
        super.onDestroy();
    }

    public class TimeThread extends Thread {
        @Override
        public void run () {
            do {
                try {
                    Thread.sleep(1000);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            beijingTime+=1000;
                            final String time = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
                            Intent timeIntent = new Intent();
                            timeIntent.setAction("currentTune");
                            timeIntent.putExtra("currentTune",time);
                            sendBroadcast(timeIntent);
                        }
                    });
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while(true);
        }
    }
}
